import { Sprite } from "@/sprites/Sprite.js";
import { Sprite_Battler } from "@/sprites/Sprite_Battler.js";
import { Sprite_SideView } from "@/sprites/Sprite_SideView.js";
import { Sprite_Weapon } from "@/sprites/Sprite_Weapon.js";
import { Sprite_StateOverlay } from "@/sprites/Sprite_StateOverlay.js";

import { ImageManager } from "@/managers/ImageManager.js";
import { BattleManager } from "@/managers/BattleManager.js";

export class Sprite_Actor extends Sprite_Battler {
    constructor() {
        super();
    }

    setBattler(actor) {
        super.setBattler(actor);
        if(this.visible){
            this.createShadowSprite();
            this.createMainSprite();
            this.createStateSprite();
            
            this.setActorHome(actor.index());
            this.startEntryMotion();
        }
    }

    createMainSprite() {
        this._mainSprite = new Sprite_SideView();
        this.addChild(this._mainSprite);
        this._effectTarget = this._mainSprite;
    }

    createShadowSprite() {
        this._shadowSprite = new Sprite();
        this._shadowSprite.anchor.set(0.5);
        this._shadowSprite.y = -2;
        this.addChild(this._shadowSprite);

        ImageManager.loadSystem('Shadow2').then(source=>{
            this._shadowSprite.source=source;
        });
    }

    createStateSprite() {
        this._stateSprite = new Sprite_StateOverlay();
        this._stateSprite.setup(actor);
        this.addChild(this._stateSprite);
    }

    launchWeapon(weaponImageId){
        if(!this.weapon){
            this.weapon=new Sprite_Weapon();
            this.addChild(this.weapon);
        }
        this.weapon.setup(weaponImageId);
    }

    _attachHandler(battler){
        super._attachHandler(battler);
        battler.on('animation-weapon',(weaponImageId)=>{
            this.launchWeapon(weaponImageId);
        });
    }

    _deattachHandler(battler){
        super._deattachHandler(battler);
        battler.off('animation-weapon')
    }

    updateMain() {
        super.updateMain();
        if (this._battler&&!this.isMoving()) {
            this.updateTargetPosition();
        }
    }

    updateTargetPosition() {
        if (this._battler.isInputting() || this._battler.isActing()) {
            this.stepForward();
        } else if (this._battler.canMove() //&& BattleManager.isEscaped()
        ) {
            this.retreat();
        } else if (!this.inHomePosition()) {
            this.stepBack();
        }
    }
    
    startEntryMotion() {
        if (this._battler && this._battler.canMove()) {
            this.startMove(0, 0, 30);
        } else if (!this.isMoving()) {
            this.startMove(0, 0, 0);
        }
    }

    moveToStartPosition() {this.startMove(300, 0, 0);}
    setActorHome(index) {this.setHome(600 + index * 32, 280 + index * 48);}
    stepForward() {this.startMove(-48, 0, 12);}
    stepBack() {this.startMove(0, 0, 12);}
    retreat() {this.startMove(300, 0, 30);}
    damageOffsetX() {return -32;}
    damageOffsetY() {return 0;}
}